package com.rocksea.app.rslib;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;

public class JySlgt extends JyCurve
{
	public JySlgt(JyDoc doc)
	{
		super(doc);

		m_strScaleX = "t (min)";
		m_strScaleY = "s (mm)";
		m_strTitle = "s-lgt曲线";
	}

	@Override
	protected void drawCurve(Canvas canvas)
	{
		Paint curPaint = new Paint();
		int nLoadGradeCount = m_jyDoc.getJyDataInfo().nLoadGradeCount;

		String strText;
		PointF ptStart = new PointF();
		PointF ptEnd = new PointF();
		RectF ellipseRect = new RectF(0f, 0f, m_style.dotStyle.dotDiameter,
				m_style.dotStyle.dotDiameter);

		curPaint.set(m_penLoad);
		curPaint.setTypeface(m_fontLable);
		curPaint.setTextSize(m_style.labelFont.fontHeight);

		Rect _rect = new Rect();
		curPaint.getTextBounds("kN", 0, "kN".length(), _rect);
		float fCharHeight = _rect.height();

		for (int i = 0; i < nLoadGradeCount; i++)
		{
			JyGradeStruct JyGrade = m_jyDoc.getJyData().get(i);
			if (JyGrade.nTime <= 0)
				continue;

			curPaint.setColor(m_style.loadCurveStyle.lineColor);
			boolean bFirstDot = true;
			for (int j = 0; j < JyGrade.RecordList.size(); j++)
			{
				short nTime = JyGrade.RecordList.get(j).nTimeCount;
				if (nTime <= 0)
					continue;

				ptEnd = getPointF(nTime, JyGrade.RecordList.get(j).fSAverage);
				if (bFirstDot)
				{
					bFirstDot = false;
				} else
				{
					canvas.drawLine(ptStart.x, ptStart.y, ptEnd.x, ptEnd.y,
							curPaint);
				}

				ptStart.x = ptEnd.x;
				ptStart.y = ptEnd.y;

				ellipseRect.left = ptStart.x - m_style.dotStyle.dotDiameter
						/ 2f;
				ellipseRect.top = ptStart.y - m_style.dotStyle.dotDiameter / 2f;
				// fix���������ȶ���ľ��������⣬�������һЩ���⴦��
				ellipseRect.right = ellipseRect.left
						+ m_style.dotStyle.dotDiameter;
				ellipseRect.bottom = ellipseRect.top
						+ m_style.dotStyle.dotDiameter;

				if (m_style.dotStyle.isFilled)
				{
					curPaint.setStyle(Paint.Style.FILL);
					canvas.drawOval(ellipseRect, curPaint);
				} else
				{
					canvas.drawOval(ellipseRect, curPaint);
				}
			}

			// д����ֵ
			curPaint.setColor(m_style.labelFont.fontColor);
			ptEnd.x += m_style.longScale * 2f;
			if (m_jyDoc.getIsGroundTest())
				strText = String.format("%.0f kPa", JyGrade.fQGrade);
			else
				strText = String.format("%.0f kN", JyGrade.fQGrade);

			canvas.drawText(strText, ptEnd.x, ptEnd.y + fCharHeight / 3f,
					curPaint);
		}
	}

	@Override
	protected void drawXCoordinate(Canvas canvas)
	{
		Paint curPaint = new Paint();
		super.drawXCoordinate(canvas);

		// д����ֵ
		curPaint.setColor(m_style.scaleFont.fontColor);

		// д��С�ĺ�����ֵ
		String strText = String.format("%.0f", m_fXMin);

		curPaint.setTypeface(m_fontScale);
		// curPaint.setStrokeWidth(m_style.scaleFont.fontHeight);
		curPaint.setTextSize(m_style.scaleFont.fontHeight);

		Rect rectText = new Rect();
		curPaint.getTextBounds(strText, 0, strText.length(), rectText);

		PointF pointText = new PointF();
		pointText.x = m_rcCurve.left - rectText.width() / 2f;
		pointText.y = m_isUp ? (m_rcCurve.bottom + m_style.longScale / 2f + rectText
				.height()) : (m_rcCurve.top - m_style.longScale * 1.5f);
		canvas.drawText(strText, pointText.x, pointText.y, curPaint);

		// д���ĺ�����ֵ
		strText = String.format("%.0f", m_fXMax);
		curPaint.getTextBounds(strText, 0, strText.length(), rectText);

		float fWidth = m_rcCurve.width() * 0.9f;
		pointText.x = m_rcCurve.left + fWidth - rectText.width() / 2f;
		canvas.drawText(strText, pointText.x, pointText.y, curPaint);

		float fMaxRight = pointText.x - rectText.width() / 2f;
		float fMinLeft = m_rcCurve.left;

		ArrayList<Short> TimeList = new ArrayList<Short>();
		TimeList.addAll(0, m_jyDoc.getJyDataInfo().LoadTimeList);
		TimeList.add((short) m_fXMax);

		for (short n : TimeList)
		{
			if (n > m_fXMax / 2f + 0.01f) // �������ʱ��,�˳�ѭ��
				continue;

			if (n < m_fXMin - 0.01f) // С��ԭ��ĺ���
				continue;

			pointText.x = getPointF(n, 0).x;
			strText = String.format("%d", n);

			curPaint.getTextBounds(strText, 0, strText.length(), rectText);
			if ((pointText.x - rectText.width() / 2f > fMinLeft + 1f)
					&& (pointText.x + rectText.width() / 2f < fMaxRight - 1f))
			{
				canvas.drawText(strText, pointText.x - rectText.width() / 2f,
						pointText.y, curPaint);
				fMinLeft = pointText.x + rectText.width() / 2f;
			}
		}

		// ����������ߣ��Ͳ��û�����̶���
		if (m_style.gridNum > 0)
			return;

		// ������̶�
		curPaint.set(m_penCoordinate);
		curPaint.setColor(m_style.coordinateStyle.lineColor);
		curPaint.setStrokeWidth(0f);

		float y1 = m_isUp ? (float) m_rcCurve.bottom : m_rcCurve.top;
		float y2 = m_isUp ? y1 + m_style.longScale : y1 - m_style.longScale;

		for (short n : TimeList)
		{
			if (n > m_fXMax + 0.01f) // �����������,�˳�ѭ��
				continue;

			if (n < m_fXMin + 0.01f) // С�ڻ����ԭ��ĺ���
				continue;

			if ((n > m_fXMax / 2f + 0.01f) && (n < m_fXMax - 0.01f)) // �������ʱ�䵫��������������Ҳ����
				continue;

			float x = getPointF(n, 0f).x;
			canvas.drawLine(x, y1, x, y2, curPaint);
		}
	}

	protected PointF getPointF(float nTime, float fS)
	{
		float fWidth = m_rcCurve.width() * 0.9f;
		float fHeight = m_rcCurve.height() * 0.9f;

		float fXFullScale = (float) (Math.log10(m_fXMax) - Math.log10(m_fXMin));
		float fYFullScale = m_fYMax - m_fYMin;

		float fXDelta = (float) ((Math.log10(nTime) - Math.log10(m_fXMin)) / fXFullScale);
		float fYDelta = (fS - m_fYMin) / fYFullScale;

		PointF pt = new PointF();
		pt.x = m_rcCurve.left + fWidth * fXDelta;
		pt.y = m_isUp ? m_rcCurve.bottom - fHeight * fYDelta : m_rcCurve.top
				+ fHeight * fYDelta;

		return pt;
	}

	@Override
	protected void calcMaxMinValue()
	{
		super.calcMaxMinValue();

		m_fXMin = 1;
		m_fXMax = 2;

		int nFirstGap = Integer.MAX_VALUE;
		for (int i = 0; i < m_jyDoc.getJyData().size(); i++)
		{
			JyGradeStruct JyGrade = m_jyDoc.getJyData().get(i);
			for (int j = 0; j < JyGrade.RecordList.size(); j++)
			{
				if (JyGrade.RecordList.get(j).nTimeCount > 0)
				{
					nFirstGap = Math.min(JyGrade.RecordList.get(j).nTimeCount,
							nFirstGap);
					break;
				}
			}
		}

		if (nFirstGap == Integer.MAX_VALUE)
			return;

		m_fXMin = nFirstGap;
		m_fXMax = m_jyDoc.getJyDataInfo().nGradeTimeMax * 2f;
	}
}
